/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.anhndt.audiobook.utils;

import com.anhndt.audiobook.entity.ItemEntity;
import com.anhndt.audiobook.ui.AudioStore;
import com.sun.lwuit.Display;
import com.sun.lwuit.Font;
import com.sun.lwuit.Image;
import java.util.Vector;
//#if BB || BB_BOLD || BB_Touch
//# import net.rim.device.api.system.CoverageInfo;
//# import net.rim.device.api.system.RadioInfo;
//#endif

/**
 *
 * @author Sun
 */
public class Util {
    // Connection Type

    public static final String VERSION_APP = "2";
    public static final int DEFAULT = 0;
    public static final int WIFI = 1;
    private static int data;
    //Licence
    public static boolean DO_LOGIN = false;
    //Account
    public static String userID;
    public static String passwd;
    public static String username;
    public static String datetime;

//    public static String AUDIO_WS = "http://localhost:81/audiows/Audio.asmx";
//    public static String AUDIO_FILE_URL = "http://localhost:81/audiows/audiofile.aspx?u={0}&a={1}&p={2}";
//    public static String RESIZE_URL = "http://localhost:81/audiows/resize.aspx?c={0}&w={1}&h={2}&u={3}";
//    public static String RESIZE_URL_WITH_QUALITY = "http://localhost:81/audiows/resize.aspx?c={0}&w={1}&h={2}&q={3}&u={4}";
//    public static String LOAD_IMAGE_URL = "http://localhost:81/audiows/loadimage.aspx?y={0}&id={1}";
    
    public static String AUDIO_WS = "http://192.168.187.1/audiows/Audio.asmx";
    public static String AUDIO_FILE_URL = "http://192.168.187.1/audiows/audiofile.aspx?u={0}&a={1}&p={2}";
    public static String RESIZE_URL = "http://192.168.187.1/audiows/resize.aspx?c={0}&w={1}&h={2}&u={3}";
    public static String RESIZE_URL_WITH_QUALITY = "http://192.168.187.1/audiows/resize.aspx?c={0}&w={1}&h={2}&q={3}&u={4}";
    public static String LOAD_IMAGE_URL = "http://192.168.187.1/audiows/loadimage.aspx?y={0}&id={1}";
    
    public static String themeID;
    public static int sizeThumb = 56, sizeThumbHit = 75;
    public static int displayCounter = 10; //default;
    //setting
    public static boolean displayThumb = true;
    public static String lastArticlePartID, lastArticleID;
    public static int lastIndexArticlePartID;
    public static ItemEntity lastArticleEntity;
    public static String connection;
    private static Vector idItemLoadedThumb = new Vector();  // nhung item nao da duoc load thumb roi, thi dua id vao Vector nay

    //TODO da detec trong skyaudio.java roi
//    static {
//        int displayWidth = Display.getInstance().getDisplayWidth();
////        int size = 40;
//        if (displayWidth < 128) {
//            imgThumbSize = 24;
//        } else if (displayWidth >= 128 && displayWidth <= 208) {
//            imgThumbSize = 32;
//        } else if (displayWidth <= 240) {
//            imgThumbSize = 48;
//        } else if (displayWidth <= 320) {
//            imgThumbSize = 56;
//        } else if (displayWidth <= 480) {
//            imgThumbSize = 64;
//        }
//        thumbTopHitSize = (int) (imgThumbSize * 1.8);
//    }
    public static void addIdItemLoadedThumb(String id) {
        if (!idItemLoadedThumb.contains(id)) {
            idItemLoadedThumb.addElement(id);
        }
    }

    public static boolean isItemLoadedThumb(String id) {
        return idItemLoadedThumb.contains(id);
    }

    public static void incrementData(int data) {
        Util.data += data;
    }

    public static int getData() {
        return data;
    }

    public static Image getImageAsResource(String path) {
        try {
            String realPath = "/res/" + Util.sizeThumb + "/" + path + ".png";
////#if BB_BOLD || BB_Touch || J2ME_78
//        realPath = "/res/78/" + path + ".png";
////#elif J2ME_40
////#         pathRes = "/res/40/" + path + ".png"
////#elif J2ME_64
////#         pathRes = "/res/64/" + path + ".png"";
////#endif
            return Image.createImage(realPath);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public static Image getImage(String id) {
        try {
            return AudioStore.getResource().getImage(id);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    /**
     * lam tron phep chia theo kieu
     * vd 5/3 =(int) = 2;
     * @param a
     * @param b
     */
    public static int divRounded(int a, int b) {
        if (b == 0) {
            return 1;
        }
        int result = a / b;
        if (a % b > 0) {
            result++;
        }
        return result;
    }
    
    public static int divRounded2(long a, int b) {
        if (b == 0) {
            return 1;
        }
        long result = a / b;
        if (a % b > 0) {
            result++;
        }
        return (int)result;
    }

    public static Font getBoldFont() {
        return AudioStore.getResource().getFont("boldFont");
    }

    public static Font getNormalFont() {
        return AudioStore.getResource().getFont("normalFont");
    }

    public static Font getSmallFont() {
        return AudioStore.getResource().getFont("smallFont");
    }

    public static String getImageUrl(String url, int width, int height, boolean crop) {
        String c = crop ? "1" : "0";
        return StringUtil.format(RESIZE_URL, new String[]{c, "" + width, "" + height, url});
    }

    public static void copy(Vector source, Vector destination) {
        if (destination == null) {
            destination = new Vector();
        } else {
            destination.removeAllElements();
        }
        for (int i = 0; i < source.size(); i++) {
            Object o = source.elementAt(i);
            destination.addElement(o);
        }
    }

    public static String getUrl(String url) {
        boolean WIFI_AVAILABLE = isWifiActived();
        if (WIFI_AVAILABLE) {
            url = getEndPoint(url, WIFI);
        }
        return url;
    }

    /**
     * Get connection string
     */
    private static String getEndPoint(String endPoint, int connectionType) {
        String s;
        switch (connectionType) {
            case WIFI:
                s = endPoint + ";interface=wifi";
                break;
            default:
                s = endPoint;
                break;
        }
        return s;
    }

    private static boolean isWifiActived() {
//#if BB_Touch || BB || BB_BOLD
//# //        return ((RadioInfo.getActiveWAFs() & (RadioInfo.WAF_WLAN)) != 0 && (CoverageInfo.isCoverageSufficient(CoverageInfo.COVERAGE_MDS,
//# //                RadioInfo.WAF_WLAN, false) || CoverageInfo.isCoverageSufficient(CoverageInfo.COVERAGE_CARRIER,
//# //                RadioInfo.WAF_WLAN, false)));
//#         return false;
//#else
        return false;
//#endif        
    }
    private static Boolean isWifiSupported;

    public static boolean isWifiSupported() {
        //#if BB_BOLD || BB || BB_Touch
//#         if (isWifiSupported == null) {
//#             isWifiSupported = (RadioInfo.areWAFsSupported(RadioInfo.WAF_WLAN)) ? Boolean.TRUE
//#                     : Boolean.FALSE;
//#         }
        //#else
        isWifiSupported = Boolean.FALSE;
        //#endif
        return isWifiSupported.booleanValue();
    }
    /**
     * Very Low Density 176x220 And Smaller
     */
    public static final int DENSITY_VERY_LOW = 10;
    /**
     * Low Density Up To 240x320
     */
    public static final int DENSITY_LOW = 20;
    /**
     * Medium Density Up To 360x480
     */
    public static final int DENSITY_MEDIUM = 30;
    /**
     * Hi Density Up To 480x854
     */
    public static final int DENSITY_HIGH = 40;
    /**
     * Very Hi Density Up To 1440x720
     */
    public static final int DENSITY_VERY_HIGH = 50;
    /**
     * HD Up To 1920x1080
     */
    public static final int DENSITY_HD = 60;

    /**
     * Returns one of the density variables appropriate for this device, notice that
     * density doesn't alwyas correspond to resolution and an implementation might
     * decide to change the density based on DPI constraints.
     *
     * @return one of the DENSITY constants of Display
     */
    public static int getDeviceDensity() {
        int d = Display.getInstance().getDisplayHeight() * Display.getInstance().getDisplayWidth();
        if (d <= 176 * 220) {
            return DENSITY_VERY_LOW;
        }
        if (d <= 240 * 320) {
            return DENSITY_LOW;
        }
        if (d <= 360 * 480) {
            return DENSITY_MEDIUM;
        }
        if (d <= 480 * 854) {
            return DENSITY_HIGH;
        }
        if (d <= 1440 * 720) {
            return DENSITY_VERY_HIGH;
        }
        return DENSITY_HD;
    }

    public static String getDeviceName() {
        String s = System.getProperty("microedition.platform");
        System.out.println("DeviceName = " + s);
        return s;
    }

    public static String getVersion() {
        return AudioStore.getInstance().getAppProperty("MIDlet-Version");
    }

    public static void gc() {
//#if BB
//#else
        System.gc();
//#endif
    }
}
